package utils;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;

public class FileUtil {
  //private static Logger log = org.apache.log4j.LogManager.getLogger(FileUtil.class);
  private static String osVersion = System.getProperty("os.name");
  private static String userName = System.getProperty("user.name");

  public static void main(String[] args) throws Exception {
    List<String> allFilesRecursive = getAllFilesRecursive(listFile("D:\\data"));
    for (String s : allFilesRecursive) {
      System.out.println(s);
    }
  }


  public static Boolean isWindows() {
    return osVersion.contains("Windows");
  }

  public static File[] listFile(String path) {
    File dir = new File(path);
    return dir.listFiles();
  }

  public static void writeText(String path, String text) {
    writeText(path, text, false);
  }

  public static void writeText(String path, String text, boolean append) {
    File dir = new File(path);

    try (Writer fileWriter = new FileWriter(path, append)) {
      fileWriter.write(text);
    } catch (IOException e) {
      // log.error(e);
    }
  }


  public static List<String> getAllFilesRecursive(File[] files) {
    List<String> fileNameList = new ArrayList<>();
    for (File file : files) {
      if (file.isDirectory()) {
        fileNameList.addAll(getAllFilesRecursive(file.listFiles())); // Calls same method again.
      } else {
        fileNameList.add(file.getAbsolutePath());
      }
    }
    return fileNameList;
  }
}
